// Copyright [2020] <unknown>
// #include <bits/stdc++.h>
#include <iostream>
using namespace std;
// #define ONLINE_JUDGE
#ifndef ONLINE_JUDGE
#define dbg(x...) { cerr << "\033[32;1m" << #x << " -> "; err(x); }
void err() { cerr << "\033[39;0m" << endl; }
template<typename T, typename... A>
void err(T a, A... x) { cerr << a << ' '; err(x...); }
#else
#define dbg(...)
#endif

#define sz(x) ((int)x.size())

const int N = 5e5+50;
typedef long long LL;
int t, n;

void doit(int aa[], int n, LL b[], bool rev = false) {
    static int st[N], top, a[N];
    memcpy(a, aa, 4 * (n+1));
    if (rev) {
        reverse(a+1, a+n+1);
    }
    top = 0;
    st[top++] = 0;
    a[0] = 0;
    LL ans = 0;
    for (int i=1; i<=n; ++i) {
        while (top > 1 && a[st[top-1]] >= a[i]) {
            LL x = st[top-1] - st[top-2], y = a[st[top-1]];
            ans -= x * y;
            --top;
        }
        LL x = i - st[top-1], y = a[i];
        ans += x * y;
        b[i] = ans;
        dbg(i, ans, x, y);
        st[top++] = i;
    }
    if (rev) {
        reverse(b+1, b+n+1);
    }
}  

int a[N];

LL ans1[N], ans2[N];

vector<int> build(int x, int aa[], int n, bool rev = false) {
    static int st[N], top, a[N];
    memcpy(a, aa, 4 * (n + 1));
    if (rev) {
        x = n - x + 1;
        reverse(a+1, a+n+1);
    }
    top = 0; st[top++] = 0; a[0] = 0;
    for (int i=1; i<=n; ++i) {
        while (top > 1 && a[st[top-1]] >= a[i]) {
            --top;
        }
        st[top++] = i;
        if (i == x) break;
    }
    vector<int> res;
    for (int i=0; i<top; ++i)
        dbg(i, st[i]);
    for (int i=1; i<top; ++i)
        for (int j=st[i-1]+1; j<=st[i]; ++j)
            res.push_back(a[st[i]]);
    return res;
}

int main(int argc, char const *argv[]) {
    // code
    int n;
    scanf("%d", &n);
    for (int i=1; i<=n; ++i) {
        scanf("%d", &a[i]);
    }
    doit(a, n, ans1); doit(a, n, ans2, true);
    int cord = -1; LL Ans = 0;
    for (int i=1; i<=n; ++i) {
        if (ans1[i] + ans2[i] - a[i] > Ans) {
            Ans = ans1[i] + ans2[i] - a[i];
            cord = i;
        }
    }
    vector<int> v1 = build(cord, a, n), v2 = build(cord, a, n, true);
    reverse(v2.begin(), v2.end());
    for (int i=1; i<sz(v2); ++i)
        v1.push_back(v2[i]);
    // cout << v1 << endl;
    for (int i=0; i<sz(v1); ++i)
        printf("%d%c", v1[i], i==sz(v1)-1? '\n':' ');
    // printf("%lld\n", Ans);

    return 0;
}
